In [2]:
import pylab as plt
In [3]:
#generating data 
mySamples= []
myLinear= []
myQuadratic= []
myCubic= []
myExponential= []

for i in range(0, 30):
    mySamples.append(i)
    myLinear.append(i)
    myQuadratic.append(i**2)
    myCubic.append(i**3)
    myExponential.append(1.5**i)
In [10]:
#SIMPLE EXAMPLE
plt.plot(mySamples, myLinear)
Out[10]:
[<matplotlib.lines.Line2D at 0x11528aa58>]

OVERLAPPING DISPLAYS

In [7]:
#All graphs in single window
plt.plot(mySamples, myLinear)
plt.plot(mySamples, myQuadratic)
plt.plot(mySamples, myCubic)
plt.plot(mySamples, myExponential)
Out[7]:
[<matplotlib.lines.Line2D at 0x11501c7f0>]

plt.figure()

  • creates a new display with that name if one does not already exist
  • if a display with that name exists, reopens it for processing

SEPARATE PLOTS

In [8]:
#To plot each graph in a seperate window 
plt.figure('lin')
plt.plot(mySamples, myLinear)
plt.figure('quad')
plt.plot(mySamples, myQuadratic)
plt.figure('cube')
plt.plot(mySamples, myCubic)
plt.figure('expo')
plt.plot(mySamples, myExponential)
Out[8]:
[<matplotlib.lines.Line2D at 0x115251c50>]

PROVIDING LABELS

In [12]:
plt.figure('lin')
plt.xlabel('sample points') #labels x axis 
plt.ylabel('linear function')
plt.plot(mySamples, myLinear)
plt.figure('quad')
plt.plot(mySamples, myQuadratic)
plt.figure('cube')
plt.plot(mySamples, myCubic)
plt.figure('expo')
plt.plot(mySamples, myExponential)
plt.figure('quad')
plt.ylabel('quadratic function')
Out[12]:
Text(0,0.5,'quadratic function')

ADDING TITLES

In [13]:
plt.figure('lin')
plt.plot(mySamples, myLinear)
plt.figure('quad')
plt.plot(mySamples, myQuadratic)
plt.figure('cube')
plt.plot(mySamples, myCubic)
plt.figure('expo')
plt.plot(mySamples, myExponential)
plt.figure('lin')
plt.title('Linear') # adds title to the graph 
plt.figure('quad')
plt.title('Quadratic')
plt.figure('cube')
plt.title('Cubic')
plt.figure('expo')
plt.title('Exponential')
Out[13]:
Text(0.5,1,'Exponential')

CLEANING UP WINDOWS

In [18]:
plt.figure('lin')
plt.clf() # helps clear and start new version , as you can see axis lable is missing 
plt.plot(mySamples, myLinear)
plt.figure('quad')
plt.clf()
plt.plot(mySamples, myQuadratic)
plt.figure('cube')
plt.clf()
plt.plot(mySamples, myCubic)
plt.figure('expo')
plt.clf()
plt.plot(mySamples, myExponential)
plt.figure('lin')
plt.title('Linear')
plt.figure('quad')
plt.title('Quadratic')
plt.figure('cube')
plt.title('Cubic')
plt.figure('expo')
plt.title('Exponential')
Out[18]:
Text(0.5,1,'Exponential')

COMPARING RESULTS

  • now suppose we would like to compare different plots
  • in particular, the scales on the graphs are very different
  • one option is to explicitly set limits on the axis or axes
  • a second option is to plot multiple functions on the same display

CHANGING LIMITS ON AXES

In [19]:
plt.figure('lin')
plt.clf()
plt.ylim(0,1000) # limits the axis 
plt.plot(mySamples, myLinear)
plt.figure('quad')
plt.clf()
plt.ylim(0,1000)
plt.plot(mySamples, myQuadratic)
plt.figure('lin')
plt.title('Linear')
plt.figure('quad')
plt.title('Quadratic')
Out[19]:
Text(0.5,1,'Quadratic')

OVERLAYING PLOTS

In [20]:
plt.figure('linquad')
plt.clf()
plt.plot(mySamples, myLinear)
plt.plot(mySamples, myQuadratic)
plt.figure('cube exp')
plt.clf()
plt.plot(mySamples, myCubic)
plt.plot(mySamples, myExponential)
plt.figure('linquad')
plt.title('Linear vs. Quadratic')
plt.figure('cube exp')
plt.title('Cubic vs. Exponential')
Out[20]:
Text(0.5,1,'Cubic vs. Exponential')

ADDING MORE DOCUMENTATION

  • can add a legend that identifies each plot
In [22]:
plt.figure('linquad')
plt.clf()
plt.plot(mySamples, myLinear, label = 'linear')
plt.plot(mySamples, myQuadratic, label = 'quadratic')
plt.legend(loc= 'upper left') #adding legend and specifying the location
plt.title('Linear vs. Quadratic')
plt.figure('cube exp')
plt.clf()
plt.plot(mySamples, myCubic, label = 'cubic')
plt.plot(mySamples, myExponential, label = 'exponential')
plt.legend()
plt.title('Cubic vs. Exponential')
Out[22]:
Text(0.5,1,'Cubic vs. Exponential')

CONTROLLING DISPLAY PARAMETERS

  • now suppose we want to control details of the displays themselves

examples:

  • changing color or style of data sets
  • changing width of lines or displays
  • using subplots
In [28]:
plt.figure('linquad')
plt.clf()
plt.plot(mySamples, myLinear, 'b-', label = 'linear') # b- blue line 
plt.plot(mySamples, myQuadratic,'ro', label = 'quadratic') # ro red dotted 
plt.legend(loc= 'upper left')
plt.title('Linear vs. Quadratic')
plt.figure('cube exp')
plt.clf()
plt.plot(mySamples, myCubic, 'g^', label = 'cubic') # green trinagle 
plt.plot(mySamples, myExponential, 'r--',label = 'exponential')# red dash
plt.legend() 
plt.title('Cubic vs. Exponential')
Out[28]:
Text(0.5,1,'Cubic vs. Exponential')

CHANGING DATA DISPLAY

In [31]:
plt.figure('linquad')
plt.clf()
plt.plot(mySamples, myLinear, 'b-', label = 'linear', linewidth= 2.0) #choosing line width 
plt.plot(mySamples, myQuadratic,'r', label = 'quadrati', linewidth= 3.0)
plt.legend(loc= 'upper left')
plt.title('Linear vs. Quadratic')
plt.figure('cube exp')
plt.clf()
plt.plot(mySamples, myCubic, 'g--', label = 'cubic', linewidth= 4.0)
plt.plot(mySamples, myExponential, 'r',label= 'exponential', linewidth= 5.0)
plt.legend()
plt.title('Cubic vs. Exponential')
Out[31]:
Text(0.5,1,'Cubic vs. Exponential')

USING SUBPLOTS

In [36]:
plt.figure('linquad')
plt.clf()
plt.subplot(211) #subplots 
plt.ylim(0,900)
plt.plot(mySamples, myLinear, 'b-', label = 'linear', linewidth= 2.0)
plt.subplot(212)
plt.ylim(0,900)
plt.plot(mySamples, myQuadratic,'r', label = 'quadratic', linewidth= 3.0)
plt.legend(loc= 'upper left')
plt.title('Linear vs. Quadratic')
plt.figure('cube exp')
plt.clf()
plt.subplot(121)
plt.ylim(0, 140000)
plt.plot(mySamples, myCubic, 'g--', label = 'cubic', linewidth= 4.0)
plt.subplot(122)
plt.ylim(0, 140000)
plt.plot(mySamples, myExponential, 'r',label= 'exponential', linewidth= 5.0)
plt.legend()
plt.title('Cubic vs. Exponential')
Out[36]:
Text(0.5,1,'Cubic vs. Exponential')

CHANGING SCALES

In [40]:
plt.figure('cube explog')
plt.clf()
plt.plot(mySamples, myCubic, 'g--', label = 'cubic', linewidth= 2.0)
plt.plot(mySamples, myExponential, 'r',label= 'exponential', linewidth= 4.0)
plt.yscale('log') # ploting on log scale on y axis
plt.legend()
plt.title('Cubic vs. Exponential')
plt.figure('cube explinear')
plt.clf()
plt.plot(mySamples, myCubic, 'g--', label = 'cubic', linewidth= 2.0)
plt.plot(mySamples, myExponential, 'r',label= 'exponential', linewidth= 4.0)
plt.legend()
plt.title('Cubic vs. Exponential')
Out[40]:
Text(0.5,1,'Cubic vs. Exponential')

Reference

  • edX course offered by MIT
  • 6.00.1x Introduction to Computer Science and Programming Using Python